// 引入一级路由组件
import Home from '@/Pages/Home'
import Login from '@/Pages/Login'
import Register from '@/Pages/Register'
import Search from '@/Pages/Search'
import Detail from '@/Pages/Detail'
import AddCartSuccess from '@/Pages/AddCartSuccess'
import ShopCart from '@/Pages/ShopCart'
import Trade from '@/Pages/Trade'
import Pay from '@/Pages/Pay'
import PaySuccess from '@/Pages/PaySuccess'
import Center from '@/Pages/Center'
import groupOrder from '@/Pages/Center/groupOrder'
import myOrder from '@/Pages/Center/myOrder'

// 路由配置信息
export default [
  {
    path: '/home',
    component: Home,
    meta: { show: true }
  },
  {
    path: '/login',
    component: Login,
    meta: { show: false }
  },
  {
    path: '/register',
    component: Register,
    meta: { show: false }
  },
  {
    path: '/search/:keyword?',
    component: Search,
    meta: { show: true },
    name: 'search',
    // 将params参数和query参数映射成属性传入路由组件
    props: route => ({ keyword3: route.params.keyword, keyword4: route.query.keyword2 })
  },
  {
    path: '/detail/:skuId',
    component: Detail,
    meta: { show: true }
  },
  {
    path: '/addCartSuccess',
    name: 'addCartSuccess',
    component: AddCartSuccess,
    beforeEnter(to, from, next) {
      // 得到要跳转到目的路由的query参数
      const skuNum = to.query.skuNum
      // 读取保存的数据
      const skuInfo = JSON.parse(window.sessionStorage.getItem('SKU_INFO_KEY'))
      console.log('---', skuNum, skuInfo)
      // 只有都存在, 才放行
      if (skuNum && skuInfo) {
        next()
      } else { // 在组件对象创建前强制跳转到首页
        next('/')
      }
    }
  },
  {
    path: '/shopCart',
    name: 'ShopCart',
    component: ShopCart,
    meta: { show: true }
  },
  {
    path: '/trade',
    name: 'Trade',
    component: Trade,
    /* 只能从购物车界面, 才能跳转到交易界面 */
    beforeEnter(to, from, next) {
      if (from.path === '/shopcart') {
        next()
      } else {
        next('/shopcart')
      }
    }
  },
  {
    path: '/pay',
    name: 'Pay',
    component: Pay,
    // 将query参数映射成props传递给路由组件
    props: route => ({ orderId: route.query.orderId }),
    /* 只能从交易界面, 才能跳转到支付界面 */
    beforeEnter(to, from, next) {
      if (from.path === '/trade') {
        next()
      } else {
        next('/trade')
      }
    }
  },
  {
    path: '/paySuccess',
    name: 'PaySuccess',
    component: PaySuccess,
    /* 只有从支付界面, 才能跳转到支付成功的界面 */
    beforeEnter(to, from, next) {
      if (from.path === '/pay') {
        next()
      } else {
        next('/pay')
      }
    }
  },
  {
    path: '/center',
    component: Center,
    meta: { show: true },
    children: [
      {
        path: 'myorder',
        component: myOrder
      },
      {
        path: 'grouporder',
        component: groupOrder
      },
      {
        path: '',
        redirect: 'myorder'
      }
    ]
  },
  {
    path: '/',
    component: Home
  }
]
